package com.liwalo.dao;

// Generated 24-ago-2013 14:46:30 by Hibernate Tools 3.6.0

import static org.hibernate.criterion.Example.create;

import java.util.List;

import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.liwalo.entities.MediosContacto;
import com.liwalo.entities.Producto;
import com.liwalo.entities.ProductosPedido;
import com.liwalo.entities.TiendaProducto;
import com.liwalo.utils.HibernateUtil;

/**
 * Home object for domain model class ProductosPedido.
 * @see com.liwalo.dao.ProductosPedido
 * @author Hibernate Tools
 */
public class ProductosPedidoDAO {

	private static final Log log = LogFactory.getLog(ProductosPedidoDAO.class);

	private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

	protected SessionFactory getSessionFactory() {
		try {
			return (SessionFactory) new InitialContext()
					.lookup("SessionFactory");
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	public void persist(ProductosPedido transientInstance) {
		log.debug("persisting ProductosPedido instance");
		Session session = sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();
		try {
			session.save(transientInstance);
	        tx.commit();
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(ProductosPedido instance) {
		log.debug("attaching dirty ProductosPedido instance");
		Session session = sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();
		try {
			session.saveOrUpdate(instance);
			 tx.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ProductosPedido instance) {
		log.debug("attaching clean ProductosPedido instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(ProductosPedido persistentInstance) {
		log.debug("deleting ProductosPedido instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ProductosPedido merge(ProductosPedido detachedInstance) {
		log.debug("merging ProductosPedido instance");
		try {
			initLazy(detachedInstance);
			ProductosPedido result = (ProductosPedido) sessionFactory
					.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public ProductosPedido findById(java.lang.Integer id) {
		log.debug("getting ProductosPedido instance with id: " + id);
		try {
			ProductosPedido instance = (ProductosPedido) sessionFactory
					.getCurrentSession().get("com.liwalo.dao.ProductosPedido",
							id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<ProductosPedido> findByExample(ProductosPedido instance) {
		log.debug("finding ProductosPedido instance by example");
		try {
			List<ProductosPedido> results = (List<ProductosPedido>) sessionFactory
					.getCurrentSession()
					.createCriteria("com.liwalo.dao.ProductosPedido")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public List<Producto> findByProducto(ProductosPedido pro) {
		log.debug("finding TiendaProducto instance by example");
		Session session = sessionFactory.getCurrentSession();
		 Transaction tx = session.beginTransaction();
		try {
			List<Producto> results = (List<Producto>) 
					session.createCriteria(ProductosPedido.class , "producto")
					.setProjection(Projections.distinct(Projections.property("producto.productoId")))
					.add(Restrictions.eq("producto.pedidoId", pro.getPedidoId()))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			 tx.commit();
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public List<ProductosPedido> findListByProductoPedido(ProductosPedido pro) {
		log.debug("finding TiendaProducto instance by example");
		Session session = sessionFactory.getCurrentSession();
		 Transaction tx = session.beginTransaction();
		try {
			List<ProductosPedido> results = (List<ProductosPedido>) 
					session.createCriteria(ProductosPedido.class , "producto")
					.add(Restrictions.eq("producto.pedidoId", pro.getPedidoId()))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			 tx.commit();
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	
	

	
	 private void initLazy(ProductosPedido pro){
			//Inicializo lazy
		        if(pro!=null){
		        	if(!Hibernate.isInitialized(pro.getPedidoId())){
		        		Hibernate.initialize(pro.getPedidoId());
		        	}
		        	
		        }
			 
		 }
		
}
